package com.itheima.tanhua.controller;


import com.itheima.tanhua.domain.UserInfo;
import com.itheima.tanhua.service.UserInfoService;
import com.itheima.tanhua.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author Lff
 * @date 2022/3/24
 * 作用：
 * 完善用户信息，更新用户信息
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserInfoService userInfoService;


    /**
     * 补充用户详情信息
     *
     * @param userInfo
     * @param
     * @return
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo) {

        //验证token是否合法
       /* if (!JwtUtils.verifyToken(authorization)) {
            return ResponseEntity.status(401).body(null);
        }*/
        //合法把token中顿出的id复制给userInfo
//        Claims claims = JwtUtils.getClaims(authorization);
//        Long id = (Long) claims.get("id");
        //从ThreadLocal中获取id
        Long id = UserHolder.getUser().getId();
        userInfo.setId(id);
        userInfoService.save(userInfo);
        return ResponseEntity.ok(null);
    }


    /**
     * 上传头像
     *
     * @return
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity head(MultipartFile headPhoto) throws IOException {
        //获取并校验token
        /*if (!JwtUtils.verifyToken(authorization)) {
            return ResponseEntity.status(401).body(null);
        }*/
//        System.out.println(":::  被调用  :::");
        //从token中获取id
//        Claims claims = JwtUtils.getClaims(authorization);
        //获取用户id
//        Long id = claims.get("id", Long.class);
        //从ThreadLocal中获取id
        Long id = UserHolder.getUser().getId();
        //调用service完善头像信息
        userInfoService.updateHead(headPhoto, id);
        return ResponseEntity.ok(null);
    }
}
